<?php

namespace app\lib\sms;

use AlibabaCloud\Client\AlibabaCloud;

// 文档：https://help.aliyun.com/document_detail/53111.html
// 安装：composer require alibabacloud/sdk
class Alisms
{
    public static function sendCode($phone, $template, $code)
    {
        AlibabaCloud::accessKeyClient(config('system.alisms.id'), config('system.alisms.secret'))
            ->regionId('cn-beijing')->asDefaultClient();
        $result = AlibabaCloud::rpc()
            ->product('Dysmsapi')
            ->version('2017-05-25')
            ->action('SendSms')
            ->method('POST')
            ->host('dysmsapi.aliyuncs.com')
            ->options([
                'query' => [
                    'PhoneNumbers' => $phone,
                    'TemplateCode' => $template,
                    'TemplateParam' => "{'code':" . $code . "}",
                    'SignName' => config('system.alisms.sign'),
                ],
            ])
            ->request();
        $result = $result->toArray();
        if (isset($result['Code']) && $result['Code'] == "OK") {
            return true;
        } else {
            return false;
        }
    }
}
